<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Busqueda extends CI_Controller {
    
    public $ruta_template;

    function __construct(){
        parent::__construct();
        $this->lang->load('lang/pro_'.$this->config->item('language'), $this->config->item('language'));
        $this->load->model('producto_model');
        $this->load->library('form_validation');
        $this->ruta_template = array('ruta' => site_url().'public/'.$this->template->getActiveTemplate(),
                                     'ruta_archivos' => 'templates/traveltemplate/');
        $this->template->write_view('footer', 'footer');
        $this->template->write_view('header', 'header');
        $this->template->write_view('_scripts', 'js', $this->ruta_template);
        $this->template->write_view('_styles', 'css', $this->ruta_template);  
    }

    public function getTemplate(){
        $row = '<tr value="%TABLEID%" bgcolor="white">';
        return array ('table_open'  => '<table id="tabla">', 'row_start'  => $row, 'row_alt_start'  => $row);
    }

    public function index(){
        $this->buscar();
    }
    
    function buscar($provincia = null, $municipio = null){
        $this->load->library('Controlador');
        $this->load->library('General');
        
        if($this->session->userdata('idusuario') == null){
            $this->session->sess_destroy();
            $this->session->sess_create(); 
        }        
        if($this->input->post('p_entorno_playa') != ''){ $this->session->set_userdata('p_entorno_playa', $this->input->post('p_entorno_playa')); }
        else{ $this->session->unset_userdata('p_entorno_playa'); }
        
        if($this->input->post('p_entorno_ciudad') != ''){ $this->session->set_userdata('p_entorno_ciudad', $this->input->post('p_entorno_ciudad')); }
        else{ $this->session->unset_userdata('p_entorno_ciudad'); }
        
        if($this->input->post('p_entorno_rural') != ''){ $this->session->set_userdata('p_entorno_rural', $this->input->post('p_entorno_rural')); }
        else{ $this->session->unset_userdata('p_entorno_rural'); }
        
        if($this->input->post('p_entorno_nieve') != ''){ $this->session->set_userdata('p_entorno_nieve', $this->input->post('p_entorno_nieve')); }
        else{ $this->session->unset_userdata('p_entorno_nieve'); }
        
        if($this->input->post('p_personas') != ''){ $this->session->set_userdata('p_personas', $this->input->post('p_personas')); }
        else{ $this->session->unset_userdata('p_personas'); }
        
        if($provincia != null){
            $this->session->set_userdata('p_valores_lugares', $provincia);
        }else{
            if($this->input->post('p_valores_lugares') != ''){ $this->session->set_userdata('p_valores_lugares', $this->input->post('p_valores_lugares')); }
            else{ $this->session->unset_userdata('p_valores_lugares'); }
        }
        
        if($this->input->post('datefrom') != ''){ $this->session->set_userdata('p_datefrom', $this->input->post('datefrom')); }
        else{ $this->session->unset_userdata('p_datefrom'); }
        
        if($this->input->post('dateto') != ''){ $this->session->set_userdata('p_dateto', $this->input->post('dateto')); }
        else{ $this->session->unset_userdata('p_dateto'); }
        
        $data['ses'] = $this->valores_sesion();
        
        $pages = 10; //Numero de registros mostrados por páginas
        $this->load->library('pagination'); //Cargamos la librería de paginación
        $config['base_url'] = site_url('busqueda/index'); // parametro base de la aplicación, si tenemos un .htaccess nos evitamos el index.php
        $config['per_page'] = $pages; 
        $config['num_links'] = 3; //Numero de links mostrados en la paginación

        $data['productos'] = $this->producto_model->buscar($config['per_page'], $this->uri->segment(3));
        $config['total_rows'] = $data['productos']->num_rows();
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['next_tag_open'] = "<li class='next'>";
        $config['next_tag_close'] = "</li>";
        $config['prev_tag_open'] = "<li class='prev'>";
        $config['prev_tag_close'] = "</li>";
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        
        $this->pagination->initialize($config); 
        
        $data['datos_tags'] = $this->producto_model->getUltimasProvinciasConReserva();
        $data['ruta_template'] = $this->template->getActiveTemplate();
        
        $this->template->write_view('content', 'content/resultado', $data);
        $this->template->render();
    }
    
    function valores_sesion(){
        $data = array();
        $data['p_entorno_playa'] = $this->session->userdata('p_entorno_playa');
        $data['p_entorno_ciudad'] = $this->session->userdata('p_entorno_ciudad');
        $data['p_entorno_rural'] = $this->session->userdata('p_entorno_rural');
        $data['p_entorno_nieve'] = $this->session->userdata('p_entorno_nieve');
        $data['p_personas'] = $this->session->userdata('p_personas');
        $data['p_valores_lugares'] = $this->session->userdata('p_valores_lugares');
        $data['p_datefrom'] = $this->session->userdata('p_datefrom');
        $data['p_dateto'] = $this->session->userdata('p_dateto');
        return $data;
    }
    
    function ver($producto = ''){
        $this->load->library('Controlador');
        $this->load->library('General');
        $data['row_producto'] = $this->producto_model->getProductos(null, $producto)->row_array();
        $data['comentario'] = $this->producto_model->get_comentarios($producto, 'PRO');
        $data['row_usuario'] = $this->producto_model->get_info_usuario($producto)->row_array();
        $query_productos_usuario = $this->producto_model->get_productos_usuario($data['row_usuario']['usu_idusuario'], $producto);
        $array_direcciones = array();
        foreach ($query_productos_usuario->result_array() as $key => $value){
            $array_direcciones[] = array($value['pro_idproducto'], $value['pro_barrio']." ".lang('en')." ".$value['mun_nombre']);
        }
        $data['direcciones'] = $array_direcciones;
        $data['foto'] = $this->producto_model->getFoto($producto);
        $data['suma_tarifa'] = $this->producto_model->get_suma_tarifas($producto);
        $data['distancias'] = $this->controlador->get_datos_producto('DISTANCIA', $producto);
        $data['distribucion'] = $this->controlador->get_datos_producto('DISTRIBUCION', $producto);
        $data['detalle'] = $this->controlador->get_datos_producto('DETALLE', $producto);
        $data['contenidos'] = $this->controlador->get_datos_producto('CONTENIDO', $producto);
        $data['municipio'] = $this->producto_model->getMunicipio($data['row_producto']['pro_provincia'], $data['row_producto']['pro_municipio']);
        $data['ses'] = $this->valores_sesion();
        $this->template->write_view('content', 'content/producto_ver', $data);
        $this->template->render();
    }
    
    function mapa(){
        $this->load->library('googlemaps');
        $config['apikey'] = 'AIzaSyDuYhYAkArYS9CTUro5Io0XJ1EFMdJ6__Q';
        $this->googlemaps->initialize($config);
        $data['map'] = $this->googlemaps->create_map();
        echo $this->load->view('google_map', $data, true);
    }
    
    function provincia($nom_provincia = null){
        $provincia = $this->producto_model->getProvinciaByNom($nom_provincia);
        $this->buscar($provincia);
    }
}

?>